//
//  YLRegularFinancingListController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/11/27.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLRegularFinancingListController.h"
#import "YLFuncDetailTableViewController.h"
#import "YLProductCollectionCell.h"
#import "YLGlobeHeader.h"
#import "YLProductDetailController.h"
#import "YLProductListViewModel.h"
#import <MJRefresh.h>
#import "YLProductModel.h"
#import "YLAccountTool.h"
@interface YLRegularFinancingListController ()<UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) YLProductListViewModel *viewModel;


@property (nonatomic, assign) NSInteger index;
@end

@implementation YLRegularFinancingListController




- (YLProductListViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLProductListViewModel alloc] init];
    }
    return _viewModel;
}

- (void)loadNewData
{
    [self.collectionView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.searchPayCategory = -1;
    self.yieldFrom = -1;
    self.yieldTo = -1;
    self.searchProductTerm = -1;
    self.searchProductCategory = -1;
    
    self.title = @"普惠";
    self.collectionView.backgroundColor = YLBackGroundColor;
    
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([YLProductCollectionCell class]) bundle:nil] forCellWithReuseIdentifier:[YLProductCollectionCell cellID]];
    
    __weak typeof(self) weakSelf = self;
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.index = 1;
        [weakSelf loadData];
    }];
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.index ++;
        [weakSelf loadData];
    }];
    
    self.collectionView.mj_footer.hidden = YES;
    
    [self.collectionView.mj_header beginRefreshing];
    
}

- (void)loadData
{
    __weak typeof(self) weakSelf = self;
    
    NSMutableDictionary *parmas = [NSMutableDictionary dictionaryWithCapacity:11];
    
    parmas[@"PageIndex"] = @(_index); // 请求页数，1开始
    parmas[@"MaxResultCount"] = @(10); // 每页条数
    parmas[@"Sorting"] = @""; // 排序键值
    parmas[@"SearchProductCategory"] = @(self.searchProductCategory); // 产品类型 企来贷 = 1, 易收益 = 2, -1
    parmas[@"SearchPayCategory"] = @(self.searchPayCategory); // 兑付方式 到期分配 = 0, 按月分配 = 1, 半年分配 = 6， -1
    parmas[@"SearchProductTerm"] = @(self.searchProductTerm); // 产品期限  3 6 12 18 24 -1
    parmas[@"IsHomeList"] = @(0); // 列表
    parmas[@"PromotionPlatformType"] = @(0); // 所有平台
    parmas[@"YieldFrom"] = @(self.yieldFrom); // 利率从，-1表示不限
    parmas[@"YieldTo"] = @(self.yieldTo); // 利率至，-1表示不限
    
    
    [[self.viewModel.requestCommand execute:parmas] subscribeNext:^(id  _Nullable x) {
        
        [weakSelf endRefresh];
        [weakSelf.collectionView reloadData];
    }];
    
    [self.viewModel.wh_loadDataErrorSubject subscribeNext:^(id  _Nullable x) {
        [weakSelf endRefresh];
    }];
    
    
    [self.viewModel.wh_noMoreDataSubject subscribeNext:^(id  _Nullable x) {
        [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
    }];
}

- (void)endRefresh
{
    self.collectionView.mj_footer.hidden = NO;
    [self.collectionView.mj_footer endRefreshing];
    [self.collectionView.mj_header endRefreshing];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    YLProductDetailController *detail = [[YLProductDetailController alloc] init];
    
    YLProductModel *model = self.viewModel.models[indexPath.item];
    detail.model = model;
    [self.navigationController pushViewController:detail animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
//
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}




#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.viewModel.models.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    YLProductModel *model = self.viewModel.models[indexPath.item];
    YLProductCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[YLProductCollectionCell cellID] forIndexPath:indexPath];
    //cell.isFund = NO;
    cell.model = model;
    cell.hiddenline = YES;
    cell.hiddenTag = NO;
    return cell;
}

#pragma mark <UICollectionViewDelegate>

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(YLScreenW, 110);
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}



@end
